import {usePreview} from "../../utils/usePreview";
import {getSchoolInfo} from "../../api/Infomange"
import {useStore} from "vuex"
import { ref ,reactive} from "vue";
export function useSchoolInfo(){
	const {state,dispatch} = useStore()
	const BASE_URL = import.meta.env.VITE_BASE_API
	const schoolImgs = reactive<any>([])
	const schoolDesc = ref<any>("")
	function inintgetSchoolInfo(){
		return new Promise((resolve,reject)=>{
			getSchoolInfo({
				OPERATOR_ID: state.user.userInfo.CUSER_ID || "0"
			})
			.then(res=>{
				schoolImgs.length = 0  //清空数组
				if(res.DAT.length!=0){
					let SchoolImageList = Array()
					schoolDesc.value = res.DAT[0].XX_TXT
					if(res.DAT[0].XX_PIC!=""){
						SchoolImageList = res.DAT[0].XX_PIC.split(";")
						let List = Array()
						for(let i = 0;i<SchoolImageList.length;i++){
							schoolImgs.push({
								url:BASE_URL + "/pic_server/browse?picfilename=" +SchoolImageList[i] //拼接图片数组路径
							})
						}
					}
				}
				resolve(res.DAT)
			})
			.catch(err=>{
				if(err.data.error =="invalid_token"){
					//判断用户是否在其他地方登录
					setTimeout(()=>{
						uni.showToast({
						    title: "你已在其他设备登录",
						    icon: 'none',
							duration: 5000
						})
						dispatch("logout")
						setTimeout(()=>{
							inintgetSchoolInfo()
						},5000)
					},1000)
				}
				reject(err)
			})
		})
	}
    const {previewImage} = usePreview()
    const banners = [
        {
            url: 'http://www.sttvu.net/public/static/image/banner-p.jpg'
        }
    ]

   

    const schoolImg = 'https://ns-strategy.cdn.bcebos.com/ns-strategy/upload/fc_big_pic/part-00553-893.jpg'

    const schoolPhoneNums = ['0754-88396853','0754-88347349']
    

    function contactSchool(){
        uni.showActionSheet({
            itemList: schoolPhoneNums,
            success: ({tapIndex}) => {
                uni.makePhoneCall({
                    phoneNumber: schoolPhoneNums[tapIndex]
                })
            }
        })
    }

    function previewSchoolImg(img: Array<{url: string}> | string, idx: number = 0) {
        previewImage(typeof img === 'string' ? img : img.map(item => item.url), idx)
    }

    return {
        schoolImg,
        schoolPhoneNums,
        banners,
        schoolDesc,
        schoolImgs,
        contactSchool,
        previewSchoolImg,
		inintgetSchoolInfo
    }
}
